package com.sykj.bsc.rwl.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.bsc.rwl.po.SysDept;
import com.sykj.bsc.rwl.po.SysRole;
import com.sykj.bsc.rwl.po.SysUser;
import com.sykj.bsc.rwl.service.SysRoleService;

/**
 * 
 * @ClassName:  SysRoleController   
 *@Description: 角色controller
 * @author 任文龙
 ** @date:   2018年8月16日 下午3:40:13
 */

@Controller
@RequestMapping("role")
public class SysRoleController {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	
	@RequestMapping("selectAllSysRole")
	public  String selectAllSysDept(Model model,HttpServletRequest req) throws Exception{
		SysRole sysRole=getSysRole(req);
		HashMap<String, String> map=new HashMap<String,String>();
		map.put("cascade_id",sysRole.getCascade_id());
		String data=sysRoleService.selectAllSysRole(map);
		model.addAttribute("msg", data);
		model.addAttribute("path","/bsc/role/selectSysRole");
		return "backstage/rwl/menu";
	}
	
	/**
	 * 
	 * @Title: selectSysRole   
	 * @Description: 查询所有角色信息  
	 * @param: @param model
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("selectSysRole")
	public String selectSysRole(Model model,HttpServletRequest req) throws Exception{
		SysRole sysRole=getSysRole(req);
		String a=req.getParameter("cascade_id");
		HashMap<String, String> map=new HashMap<String,String>();
		map.put("cascade_id", sysRole.getCascade_id());
		map.put("data", a);
		List<SysRole> list=sysRoleService.selectSysRole(map);
		model.addAttribute("list", list);
		/*List<SysRole> list2=sysRoleService.selectSysRole(null);
		model.addAttribute("list2", list2);*/
		return "backstage/rwl/sysRole";
	}
	
	/**
	 * @Title: getSysRole   
	 * @Description: 通过用户id获取角色信息  
	 * @param: @param req
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: SysRole      
	 * @throws
	 */
	private SysRole getSysRole(HttpServletRequest req) throws Exception{
		SysUser sysuser= (SysUser) req.getSession().getAttribute("user");
		String user_id=sysuser.getUser_id()+"";
		HashMap<String, String> map=new HashMap<String,String>();
		map.put("user_id", user_id);
		SysRole sysRole=sysRoleService.findBySysRole(map);
		return sysRole;
	}
	
	/**
	 * @Title: deleteSysRole   
	 * @Description: 删除角色   
	 * @param: @param role_id
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("deleteSysRole")
	public String deleteSysRole(@RequestParam("role_id")Integer role_id) throws Exception {
		sysRoleService.deleteSysRole(role_id);
		return "redirect:selectSysRole";
	}
	
	/**
	 * @Title: insertSysRole   
	 * @Description: 新增角色  和修改
	 * @param: @param sysRole
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("savetSysRole")
	public String savetSysRole(SysRole sysRole,HttpServletRequest req) throws Exception {
		HttpSession session=req.getSession();
		SysUser sysUser=(SysUser) session.getAttribute("user");
		if(sysRole.getRole_id()==0) {
			sysRole.setCreate_user_id(sysUser.getUser_id()+"");
			sysRoleService.insertSysRole(sysRole);
		}else {
			sysRole.setModify_user_id(sysUser.getUser_id()+"");
			sysRoleService.updateSysRole(sysRole);
		}
		return "redirect:selectSysRole";
	}
	
	/**
	 * @Title: querySysRole   
	 * @Description: 查询所有角色以json个格式返回  
	 * @param: @param req
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: List<SysRole>      
	 * @throws
	 */
	@RequestMapping("querySysRole")
	public @ResponseBody List<SysRole> querySysRole(HttpServletRequest req) throws Exception{
		SysRole sysRole=getSysRole(req);
		HashMap<String, String> map=new HashMap<String,String>();
		map.put("cascade_id", sysRole.getCascade_id());
		List<SysRole> list=sysRoleService.selectSysRole(map);
		return list;
	}
	
	/**
	 * @Title: updateSysRole   
	 * @Description: 通过角色id查询角色信息  
	 * @param: @param role_id
	 * @param: @param resp
	 * @param: @throws Exception      
	 * @return: void      
	 * @throws
	 */
	@RequestMapping("updateSysRole")
	public void updateSysRole(@RequestParam("role_id") int role_id,HttpServletResponse resp) throws Exception {
		HashMap<String, String> map=new HashMap<>();
		map.put("role_id2", role_id+"");
		SysRole sysRole=sysRoleService.findByIdSysRole(map);
		SysUserController.getWirte(resp, sysRole);
	}
	
	@RequestMapping("findByIdSysRole")
	public void findByIdSysUser(@RequestParam("role_name")String role_name,
			@RequestParam("role_name2")String role_name2,HttpServletResponse resp ) throws Exception {
		System.out.println(role_name+","+role_name2);
		HashMap<String, String> map=new HashMap<>();
		HashMap<String, Boolean> map2=new HashMap<>();
		map.put("role_name", role_name);
		SysRole sysRole=sysRoleService.findByIdSysRole(map);
		System.out.println(sysRole);
		if(sysRole==null) {
			map2.put("valid", true);
		}else {
			if(role_name.equals(role_name2)) {
				map2.put("valid", true);
			}else {
				map2.put("valid", false);
			}
		}
		SysUserController.getWirte(resp, map2);
	}
}